<meta charset='utf-8' />
<title>地区选择</title>

<h1 style='text-align:center;color:red'>地区选择</h1>

<div id='cityArea' style='text-align:center'>
	<select id="sheng">xxx</select>
	<select id="shi">xxx</select>
	<select id="xian">xxx</select>
</div>
<div style='margin-top:30px;text-align:center'>
	<button id='changeBtn'>切换</button>
</div>



<script src='./public/fun.js'></script>
<script src='./public/jquery.js'></script>
<script>
_getCityList(function(res){
	_parseCityList(res,sheng,shi,xian,function(){
		$('#changeBtn').click(function(){
			sessionStorage.cityName = xian.options[xian.selectedIndex].text;
			sessionStorage.cityCode = xian.options[xian.selectedIndex].value;
			_jumpMain2();
		});
	});
})


function _parseCityList(proList,ele_province,ele_city,ele_county,fun){
	PushProvince(proList,ele_province,ele_city,ele_county,fun);
 	//填充省数据
	function PushProvince(proList,ele_province,ele_city,ele_county,fun){
		ele_province.innerHTML = ''
		var Province_Str = '';
		for(var i=0;i<proList.length;i++){
			Province_Str += "<option value="+proList[i][0]+">"+proList[i][0]+"</option>";
		}
		ele_province.innerHTML=Province_Str;
		PushCity(proList[ele_province.selectedIndex][1],ele_city,ele_county,fun);		//直充市数据
		ele_province.onchange = function(){
			PushCity(proList[ele_province.selectedIndex][1],ele_city,ele_county,fun); 	//直充市数据
		}
	}

	//填充市数据
	function PushCity(cityList,ele_city,ele_county,fun){
		ele_city.innerHTML = ''
		var City_Str = '';
		for(var i=0;i<cityList.length;i++){
			City_Str += "<option value="+cityList[i][0]+">"+cityList[i][0]+"</option>";
		}
		ele_city.innerHTML = City_Str;
		PushCounty(cityList[ele_city.selectedIndex][1],ele_county,fun);		//直充市数据
		ele_city.onchange = function(){
			PushCounty(cityList[ele_city.selectedIndex][1],ele_county,fun); 	//直充市数据
		}

	}

	//填充县数据
	function PushCounty(countyList,ele_county,fun){
		ele_county.innerHTML = '';
		var County_Str = '';
		for(var i=0;i<countyList.length;i++){
			County_Str += "<option value="+countyList[i][0]+">"+countyList[i][1]+"</option>";
		}
		ele_county.innerHTML = County_Str;
		if(fun){
			fun();
		}
	}
}






function _getCityList(fun,type){
	if(localStorage.cityList){
		if(type)C('cityList from storage');
		fun(JSON.parse(localStorage.cityList));
	}else{
		Ajax({},function(res){
			if(type)C('cityList from after');
			if(/^\[.*\]$/.test(res)){
				localStorage.cityList=res;
				fun(JSON.parse(localStorage.cityList));
			}
		},'./public/cityJson')
	}
}
</script>